/***************************************************************************
*   Copyright (C) 2007 by Yuri Ovcharenko                                 *
*   amwsoft@gmail.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "at91sam7s.h"

#include <QtGui>
#include <math.h>

/* XPM */
static char * guess_xpm[] = {
"32 32 587 2",
"  	c None",
". 	c #F8C463",
"+ 	c #F6CC8A",
"@ 	c #F6D2A6",
"# 	c #F8D9B7",
"$ 	c #FADEBB",
"% 	c #FBD99E",
"& 	c #F9CF74",
"* 	c #F9CA71",
"= 	c #F7CF92",
"- 	c #F6D5AC",
"; 	c #F8DBBB",
"> 	c #FBE0BF",
", 	c #FBDCA5",
"' 	c #F9D380",
") 	c #EDC26C",
"! 	c #FCE5B8",
"~ 	c #FDE9C4",
"{ 	c #FDEAC9",
"] 	c #FCEBD2",
"^ 	c #FBEBD9",
"/ 	c #FCEEE0",
"( 	c #FDF0E2",
"_ 	c #FDF0DA",
": 	c #FDEECE",
"< 	c #FAEACA",
"[ 	c #F3E3C4",
"} 	c #E6D0A9",
"| 	c #F5BC5A",
"1 	c #F9D8A5",
"2 	c #FDF0E1",
"3 	c #FEFAF5",
"4 	c #FFFFFF",
"5 	c #FFFDF4",
"6 	c #FDF6E2",
"7 	c #EEDDB9",
"8 	c #D7BB87",
"9 	c #F4BB6B",
"0 	c #F9D5A0",
"a 	c #FCE8C8",
"b 	c #FEF6E8",
"c 	c #FFFCF4",
"d 	c #FFFFF8",
"e 	c #FFFFF9",
"f 	c #FFFEF7",
"g 	c #FFFEF6",
"h 	c #FFFDF5",
"i 	c #FFFDF3",
"j 	c #FFFCEB",
"k 	c #FFF8DE",
"l 	c #F8EBC7",
"m 	c #EDD9A9",
"n 	c #DDBA7A",
"o 	c #C6903F",
"p 	c #EA9116",
"q 	c #EA9533",
"r 	c #F3BF84",
"s 	c #FDE9D1",
"t 	c #FFF4E2",
"u 	c #FFF7E2",
"v 	c #FFF9E2",
"w 	c #FFF9E0",
"x 	c #FFF9E1",
"y 	c #FFF8E2",
"z 	c #FFF8E1",
"A 	c #FFF7DF",
"B 	c #FFF6DA",
"C 	c #FFF4D5",
"D 	c #FFF3D1",
"E 	c #FFF2CD",
"F 	c #FFF5CD",
"G 	c #FFF0C0",
"H 	c #F6CF86",
"I 	c #E8AA4B",
"J 	c #C28830",
"K 	c #E78C20",
"L 	c #EA9642",
"M 	c #F4BF85",
"N 	c #FEE9C5",
"O 	c #FFEFCE",
"P 	c #FEEDC8",
"Q 	c #FEEECA",
"R 	c #FDEFCB",
"S 	c #FEF0CC",
"T 	c #FFF1CC",
"U 	c #FFF1CB",
"V 	c #FFF0C9",
"W 	c #FFEEC4",
"X 	c #FEEBBE",
"Y 	c #FCE8B9",
"Z 	c #FCE6B4",
"` 	c #FFEBB5",
" .	c #FFEAAC",
"..	c #FED381",
"+.	c #F6B852",
"@.	c #DA972F",
"#.	c #B06D0A",
"$.	c #E48100",
"%.	c #E68922",
"&.	c #EA994B",
"*.	c #F5BF7C",
"=.	c #FFE3A8",
"-.	c #FEE5AE",
";.	c #FADEA9",
">.	c #F4DCAD",
",.	c #F0DCB1",
"'.	c #F7E3B3",
").	c #FFEAB4",
"!.	c #FFEBB3",
"~.	c #FFE9B1",
"{.	c #FDE5AD",
"].	c #F9E0A7",
"^.	c #F1D79F",
"/.	c #ECD197",
"(.	c #F5D895",
"_.	c #FFDE8D",
":.	c #FFD473",
"<.	c #FFC153",
"[.	c #EAA12A",
"}.	c #D38207",
"|.	c #E47E00",
"1.	c #E17700",
"2.	c #E58518",
"3.	c #EB9C3B",
"4.	c #F7C068",
"5.	c #FFDE90",
"6.	c #FBDD97",
"7.	c #EED093",
"8.	c #D5BE91",
"9.	c #C5B590",
"0.	c #DFCB96",
"a.	c #FFE59D",
"b.	c #FFEA9D",
"c.	c #FFE69A",
"d.	c #F8DE97",
"e.	c #ECD290",
"f.	c #CFB984",
"g.	c #BEA979",
"h.	c #DCC17A",
"i.	c #FFDB77",
"j.	c #FFD35F",
"k.	c #FCBF41",
"l.	c #F3A61D",
"m.	c #E68F03",
"n.	c #C67902",
"o.	c #E48000",
"p.	c #E07300",
"q.	c #E5830C",
"r.	c #ED9F24",
"s.	c #F9C04E",
"t.	c #FFD974",
"u.	c #F5D57E",
"v.	c #E0C27D",
"w.	c #B2A075",
"x.	c #958B6E",
"y.	c #C5B379",
"z.	c #FFE285",
"A.	c #FFEB86",
"B.	c #FFE583",
"C.	c #F3D880",
"D.	c #DCC379",
"E.	c #AA9969",
"F.	c #8B7F5B",
"G.	c #C0AA5F",
"H.	c #FFD95F",
"I.	c #FFD246",
"J.	c #F7BB28",
"K.	c #F9AB0F",
"L.	c #F59B00",
"M.	c #CD8103",
"N.	c #E78C00",
"O.	c #E37D00",
"P.	c #E78904",
"Q.	c #EE9F10",
"R.	c #FABD2D",
"S.	c #FFD34C",
"T.	c #EFCC5C",
"U.	c #D2B563",
"V.	c #98885C",
"W.	c #756E54",
"X.	c #B4A35F",
"Y.	c #FFE16B",
"Z.	c #FFEE6C",
"`.	c #FFE669",
" +	c #ECD466",
".+	c #CDB660",
"++	c #8F8151",
"@+	c #696142",
"#+	c #AE9842",
"$+	c #FED640",
"%+	c #FFD02A",
"&+	c #F7B912",
"*+	c #FBAF05",
"=+	c #F9A500",
"-+	c #D79003",
";+	c #A9760B",
">+	c #F3B100",
",+	c #EB9800",
"'+	c #E78B00",
")+	c #EA9200",
"!+	c #EFA100",
"~+	c #FBBA11",
"{+	c #FFCD26",
"]+	c #EAC43A",
"^+	c #C7AC47",
"/+	c #877943",
"(+	c #615B3D",
"_+	c #AA9947",
":+	c #FFE251",
"<+	c #FFF251",
"[+	c #FFE94E",
"}+	c #E8D14C",
"|+	c #C3AD47",
"1+	c #7E723A",
"2+	c #564F2C",
"3+	c #A38D27",
"4+	c #FDD322",
"5+	c #FFD011",
"6+	c #F8BA01",
"7+	c #FBB400",
"8+	c #F9AD00",
"9+	c #E19D03",
"0+	c #C58C06",
"a+	c #F1A400",
"b+	c #ED9B00",
"c+	c #EB9700",
"d+	c #EFA000",
"e+	c #F5AE00",
"f+	c #FFC004",
"g+	c #FFCB0E",
"h+	c #E9C01A",
"i+	c #C4A624",
"j+	c #817126",
"k+	c #5B5326",
"l+	c #A7972E",
"m+	c #FFE636",
"n+	c #FFF636",
"o+	c #FFEC33",
"p+	c #E8D331",
"q+	c #C3AE2C",
"r+	c #7C6F21",
"s+	c #524916",
"t+	c #A08811",
"u+	c #FDD30D",
"v+	c #FFD405",
"w+	c #FCC300",
"x+	c #FDBC00",
"y+	c #FAB400",
"z+	c #E9A602",
"A+	c #D59804",
"B+	c #B78308",
"C+	c #F09B00",
"D+	c #F09E00",
"E+	c #F0A300",
"F+	c #F0AB00",
"G+	c #F2B500",
"H+	c #FBC300",
"I+	c #FECA00",
"J+	c #EBC003",
"K+	c #CAAA07",
"L+	c #8F7A0D",
"M+	c #6D6013",
"N+	c #B0A019",
"O+	c #FFE91D",
"P+	c #FFF81D",
"Q+	c #FFEE1B",
"R+	c #ECD919",
"S+	c #CCB715",
"T+	c #8C7D0E",
"U+	c #665906",
"V+	c #AC9302",
"W+	c #FDD600",
"X+	c #FFD700",
"Y+	c #F7C600",
"Z+	c #F8C000",
"`+	c #F8B900",
" @	c #EFAE01",
".@	c #E1A203",
"+@	c #BD8908",
"@@	c #EE9801",
"#@	c #F5A401",
"$@	c #F6AF00",
"%@	c #E0A500",
"&@	c #CD9F00",
"*@	c #E0B400",
"=@	c #F6CA00",
"-@	c #F2CB00",
";@	c #E3C300",
">@	c #C5AB03",
",@	c #B29D07",
"'@	c #D6C00A",
")@	c #FFE80B",
"!@	c #FFF10B",
"~@	c #FFEC0A",
"{@	c #F5E109",
"]@	c #E5CF07",
"^@	c #C4AF04",
"/@	c #B09C01",
"(@	c #D6BD00",
"_@	c #FEDF00",
":@	c #EECC00",
"<@	c #D8B100",
"[@	c #E3B200",
"}@	c #F1B700",
"|@	c #F1B201",
"1@	c #E8A903",
"2@	c #C18D09",
"3@	c #EE9802",
"4@	c #FAAB01",
"5@	c #FBB800",
"6@	c #CC9C00",
"7@	c #A58400",
"8@	c #C3A200",
"9@	c #EDCA00",
"0@	c #F9D800",
"a@	c #FCDE00",
"b@	c #FADE00",
"c@	c #F7DC00",
"d@	c #FBE200",
"e@	c #FFE800",
"f@	c #FFEA00",
"g@	c #FFEB00",
"h@	c #FEEA00",
"i@	c #FDE700",
"j@	c #FAE300",
"k@	c #F8E000",
"l@	c #FFE900",
"m@	c #D6BE00",
"n@	c #B39700",
"o@	c #CAA100",
"p@	c #EBB500",
"q@	c #F2B501",
"r@	c #ECAD03",
"s@	c #C28F09",
"t@	c #F09F01",
"u@	c #FDB201",
"v@	c #C49700",
"w@	c #947900",
"x@	c #B79C00",
"y@	c #EACC00",
"z@	c #FBE000",
"A@	c #FFEE00",
"B@	c #FFEF00",
"C@	c #FFF000",
"D@	c #FFF100",
"E@	c #FFF200",
"F@	c #FFF300",
"G@	c #FFF800",
"H@	c #C9B700",
"I@	c #9A8400",
"J@	c #BC9800",
"K@	c #EBB700",
"L@	c #F4B801",
"M@	c #ECAE03",
"N@	c #C18F09",
"O@	c #F3A900",
"P@	c #FFB900",
"Q@	c #FDBE00",
"R@	c #C29600",
"S@	c #8E7500",
"T@	c #AE9600",
"U@	c #DFC500",
"V@	c #F4E000",
"W@	c #FFF400",
"X@	c #FFF500",
"Y@	c #FFF600",
"Z@	c #FFFB00",
"`@	c #BCAE00",
" #	c #877500",
".#	c #B49200",
"+#	c #EFBC00",
"@#	c #F5BA01",
"##	c #E8AB03",
"$#	c #BD8C09",
"%#	c #FAB702",
"&#	c #FFC001",
"*#	c #FEBE00",
"=#	c #8A7200",
"-#	c #948000",
";#	c #B6A300",
">#	c #DFD200",
",#	c #FFFE00",
"'#	c #FFF700",
")#	c #FFFC00",
"!#	c #F0E200",
"~#	c #AA9D00",
"{#	c #766700",
"]#	c #B09000",
"^#	c #F6C400",
"/#	c #F5BE02",
"(#	c #DEA705",
"_#	c #B5880A",
":#	c #FFC405",
"<#	c #FFC402",
"[#	c #FEBC00",
"}#	c #CB9C00",
"|#	c #957B00",
"1#	c #837100",
"2#	c #8C7E00",
"3#	c #C5BD00",
"4#	c #FFFF00",
"5#	c #FFF900",
"6#	c #FFFA00",
"7#	c #F1E900",
"8#	c #D6C900",
"9#	c #988B00",
"0#	c #706100",
"a#	c #B39400",
"b#	c #FCCE00",
"c#	c #F1C003",
"d#	c #CFA007",
"e#	c #A8810C",
"f#	c #FBBA03",
"g#	c #FFBA00",
"h#	c #E8B200",
"i#	c #C3A100",
"j#	c #8F7A00",
"k#	c #736700",
"l#	c #AAA100",
"m#	c #EEE600",
"n#	c #FAFA00",
"o#	c #D5CF00",
"p#	c #AA9F00",
"q#	c #867800",
"r#	c #7C6A00",
"s#	c #C0A400",
"t#	c #FFDB00",
"u#	c #E7C005",
"v#	c #B28D0B",
"w#	c #EBAA03",
"x#	c #FDB700",
"y#	c #FFC500",
"z#	c #F1C700",
"A#	c #AA9000",
"B#	c #706200",
"C#	c #948800",
"D#	c #CFC200",
"E#	c #EDE600",
"F#	c #E8E800",
"G#	c #B3AF00",
"H#	c #847A00",
"I#	c #817200",
"J#	c #978300",
"K#	c #D2B800",
"L#	c #FFE100",
"M#	c #D8BB07",
"N#	c #8F7511",
"O#	c #EFAE04",
"P#	c #FEC501",
"Q#	c #FFCF00",
"R#	c #CEAB00",
"S#	c #998200",
"T#	c #8A7900",
"U#	c #918500",
"V#	c #B4AD00",
"W#	c #DDDA00",
"X#	c #F7F500",
"Y#	c #F0EF00",
"Z#	c #D1D000",
"`#	c #AEAC00",
" $	c #898400",
".$	c #786D00",
"+$	c #9E8E00",
"@$	c #CFBA00",
"#$	c #E9CE01",
"$$	c #F1D103",
"%$	c #C3A70A",
"&$	c #D8A109",
"*$	c #F2BC04",
"=$	c #FFCC00",
"-$	c #EEC300",
";$	c #CCAC00",
">$	c #907B00",
",$	c #655900",
"'$	c #817800",
")$	c #B3AB00",
"!$	c #DDD800",
"~$	c #FEFB00",
"{$	c #EFEF00",
"]$	c #D4D200",
"^$	c #A29F00",
"/$	c #787300",
"($	c #726900",
"_$	c #867900",
":$	c #C6B500",
"<$	c #F7DA03",
"[$	c #D9B607",
"}$	c #A5890E",
"|$	c #E7B007",
"1$	c #FBC203",
"2$	c #FCCC01",
"3$	c #EECA00",
"4$	c #BEA200",
"5$	c #8C7900",
"6$	c #7D6F00",
"7$	c #827700",
"8$	c #9B9400",
"9$	c #B7B200",
"0$	c #C0BD00",
"a$	c #BDBB00",
"b$	c #ADAA00",
"c$	c #979200",
"d$	c #7D7600",
"e$	c #726800",
"f$	c #968A00",
"g$	c #C6B700",
"h$	c #EDD800",
"i$	c #FFE601",
"j$	c #DEC206",
"k$	c #A4890E",
"l$	c #D69F0A",
"m$	c #F1B506",
"n$	c #FDCB03",
"o$	c #FFD800",
"p$	c #EAC800",
"q$	c #C8AB00",
"r$	c #958000",
"s$	c #6C5E00",
"t$	c #706400",
"u$	c #807500",
"v$	c #877F00",
"w$	c #898200",
"x$	c #7C7500",
"y$	c #716800",
"z$	c #746800",
"A$	c #887A00",
"B$	c #FFE901",
"C$	c #ECCE03",
"D$	c #B49B0B",
"E$	c #564818",
"F$	c #D39F0A",
"G$	c #E3B308",
"H$	c #ECC006",
"I$	c #F1C703",
"J$	c #EEC600",
"K$	c #D2B200",
"L$	c #B69D00",
"M$	c #B49B00",
"N$	c #B9A000",
"O$	c #BBA400",
"P$	c #BBA800",
"Q$	c #B8A500",
"R$	c #B7A400",
"S$	c #BEAC00",
"T$	c #CBB800",
"U$	c #E2CE02",
"V$	c #F2DC05",
"W$	c #DCC408",
"X$	c #837011",
"Y$	c #CC9D0C",
"Z$	c #E9B907",
"`$	c #FFD002",
" %	c #FFD901",
".%	c #FFDC00",
"+%	c #FDD900",
"@%	c #FAD400",
"#%	c #F7D400",
"$%	c #F6D600",
"%%	c #FADD00",
"&%	c #FFE400",
"*%	c #EBD305",
"=%	c #CDB40A",
"-%	c #9B8611",
";%	c #CFA30B",
">%	c #DAB10A",
",%	c #E2BB08",
"'%	c #E7C007",
")%	c #E8C005",
"!%	c #E9BF04",
"~%	c #E8C004",
"{%	c #E7C204",
"]%	c #E7C705",
"^%	c #E6CA07",
"/%	c #DFC708",
"(%	c #D2BD0A",
"_%	c #BBA70E",
":%	c #9B8712",
"<%	c #947516",
"[%	c #AD8710",
"}%	c #BA910C",
"|%	c #C19809",
"1%	c #C49C09",
"2%	c #C39D0A",
"3%	c #BD9C0C",
"4%	c #B0950F",
"5%	c #937E14",
"6%	c #6E5E1A",
"                          . + @ # $ % &                         ",
"                          * = - ; > , ' )                       ",
"                      ! ~ { ] ^ / ( _ : < [ }                   ",
"                  | 1 2 3 4 4 4 4 4 4 4 5 6 7 8                 ",
"                9 0 a b c d e d f g h i j k l m n o             ",
"            p q r s t u v w x y z A B C D E F G H I J           ",
"            K L M N O P Q R S T U V W X Y Z `  ...+.@.#.        ",
"          $.%.&.*.=.-.;.>.,.'.).!.~.{.].^./.(._.:.<.[.}.        ",
"        |.1.2.3.4.5.6.7.8.9.0.a.b.c.d.e.f.g.h.i.j.k.l.m.n.      ",
"        o.p.q.r.s.t.u.v.w.x.y.z.A.B.C.D.E.F.G.H.I.J.K.L.M.      ",
"        N.O.P.Q.R.S.T.U.V.W.X.Y.Z.`. +.+++@+#+$+%+&+*+=+-+;+    ",
"      >+,+'+)+!+~+{+]+^+/+(+_+:+<+[+}+|+1+2+3+4+5+6+7+8+9+0+    ",
"      a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z+A+B+  ",
"      C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z+`+ @.@+@  ",
"      @@#@$@%@&@*@=@-@;@>@,@'@)@!@~@{@]@^@/@(@_@:@<@[@}@|@1@2@  ",
"      3@4@5@6@7@8@9@0@a@b@c@d@e@f@g@h@i@j@k@l@e@m@n@o@p@q@r@s@  ",
"      t@u@x+v@w@x@y@z@f@A@A@A@A@A@B@C@D@E@F@G@C@H@I@J@K@L@M@N@  ",
"      O@P@Q@R@S@T@U@V@D@W@E@F@W@W@W@W@X@Y@Y@Z@C@`@ #.#+#@###$#  ",
"      %#&#*#R@=#-#;#>#Z@,#Y@'#G@G@G@G@G@Z@)#'#!#~#{#]#^#/#(#_#  ",
"      :#<#[#}#|#1#2#3#Z@4#G@5#Z@)#)#Z@6#,#)#7#8#9#0#a#b#c#d#e#  ",
"        f#g#h#i#j#k#l#m#)#4#4#,#,#,#4#4#4#n#o#p#q#r#s#t#u#v#    ",
"        w#x#y#z#A#B#C#D#E#4#4#4#4#4#4#4#n#F#G#H#I#J#K#L#M#N#    ",
"          O#P#Q#R#S#T#U#V#W#X#4#4#4#4#Y#Z#`# $.$+$@$#$$$%$      ",
"          &$*$=$-$;$>$,$'$)$!$~$4#4#{$]$^$/$($_$:$l@<$[$}$      ",
"            |$1$2$3$4$5$6$7$8$9$0$a$b$c$d$e$f$g$h$i$j$k$        ",
"            l$m$n$o$p$q$r$s$t$u$v$w$x$y$z$A$g$A@B$C$D$E$        ",
"              F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$D$X$          ",
"                  Y$Z$`$ %.%+%@%#%$%%%&%f@B$*%=%-%              ",
"                    ;%>%,%'%)%!%~%{%]%^%/%(%_%:%                ",
"                        <%[%}%|%1%2%3%4%5%6%                    ",
"                                                                ",
"                                                                "
};

AT91SAM7S::AT91SAM7S( QWidget *parent )
		: QFrame( parent )
{
	QVBoxLayout *lv;
	QGridLayout *lg;
	QGroupBox *gbox;
	QFrame *f;

	/***************************************************************************/
	setWindowTitle(tr("AT91SAM7S"));

	/***************************************************************************/
	lv = new QVBoxLayout(this);
	gbox = new QGroupBox("PLL Calculation");
	lv->addWidget(gbox);


	/***************************************************************************/
	lg = new QGridLayout(gbox);

	lg->addWidget(new QLabel("PLLCK = MAINCK"), 1, 0, Qt::AlignVCenter);

	mainck = new QSpinBox();
	mainck->setRange(3000000, 30000000);
	mainck->setValue(18432000);
	mainck->setSingleStep(1000);
	lg->addWidget(mainck, 1, 1, Qt::AlignVCenter);

	lg->addWidget(new QLabel("*"), 1, 2, Qt::AlignVCenter);
	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 1, 3, 1, 3, Qt::AlignVCenter);

	lg->addWidget(new QLabel("MUL"), 0, 3, Qt::AlignVCenter);
	mul = new QSpinBox();
	mul->setRange(0, 2047);
	mul->setValue(0);
	lg->addWidget(mul, 0, 4, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" + 1"), 0, 5, Qt::AlignVCenter);

	lg->addWidget(new QLabel("DIV"), 2, 3, Qt::AlignVCenter);
	div = new QSpinBox();
	div->setRange(1, 255);
	div->setValue(1);
	lg->addWidget(div, 2, 4, 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" = "), 1, 6, Qt::AlignVCenter | Qt::AlignRight);

	pllck = new QDoubleSpinBox();
	pllck->setReadOnly(true);
	pllck->setRange(0.0, 300000000.0);
	pllck->setValue(0.0);
	pllck->setDecimals(3);
	lg->addWidget(pllck, 1, 7, Qt::AlignVCenter);

	pllMin = new QSpinBox();
	pllMin->setRange(0, 300000000);
	pllMin->setValue(80000000);
	pllMin->setSingleStep(1000000);
	pllMin->setPrefix("Min: ");
	lg->addWidget(pllMin, 0, 7);

	pllMax = new QSpinBox();
	pllMax->setRange(0, 300000000);
	pllMax->setValue(180000000);
	pllMax->setSingleStep(1000000);
	pllMax->setPrefix("Max: ");
	lg->addWidget(pllMax, 2, 7);

	/***************************************************************************/
	gbox = new QGroupBox("MCK Calculation");
	lv->addWidget(gbox);

	lg = new QGridLayout(gbox);

	lg->addWidget(new QLabel("MCK = "), 1, 0, Qt::AlignVCenter);

	lg->addWidget(new QLabel("PLLCK"), 0, 1, 1, 2, Qt::AlignVCenter | Qt::AlignHCenter);

	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 1, 1, 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("PRES"), 2, 1);
	mckPrescaler = new QSpinBox();
	mckPrescaler->setRange(0, 6);
	lg->addWidget(mckPrescaler, 2, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" = "), 1, 3, Qt::AlignVCenter | Qt::AlignRight);

	mck = new QDoubleSpinBox();
	mck->setReadOnly(true);
	mck->setRange(0.0, 100000000.0);
	mck->setDecimals(3);
	lg->addWidget(mck, 1, 4);

	/***************************************************************************/
	gbox = new QGroupBox("USB Calculation");
	lv->addWidget(gbox);

	lg = new QGridLayout(gbox);

	lg->addWidget(new QLabel("UDPCK = "), 1, 0, Qt::AlignVCenter);

	lg->addWidget(new QLabel("PLLCK"), 0, 1, 1, 2, Qt::AlignVCenter | Qt::AlignHCenter);

	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 1, 1, 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("USBDIV"), 2, 1, Qt::AlignVCenter);
	udpPrescaler = new QSpinBox();
	udpPrescaler->setRange(0, 2);
	lg->addWidget(udpPrescaler, 2, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" = "), 1, 3, Qt::AlignVCenter | Qt::AlignRight);
	udpck = new QDoubleSpinBox();
	udpck->setReadOnly(true);
	udpck->setRange(0.0, 200000000.0);
	udpck->setDecimals(3);
	lg->addWidget(udpck, 1, 4, Qt::AlignVCenter);

	QPushButton *b = new QPushButton(QIcon(guess_xpm), "Guess");
	connect(b, SIGNAL(clicked()), this, SLOT(usbGuess()));
	lg->addWidget(b, 1, 5);

	uProgress = new QProgressBar();
	uProgress->setRange(0, 2048 * 256);
	uProgress->setValue(0);
	lg->addWidget(uProgress, 0, 4, 1, 2);

	lg->addWidget(new QLabel("Error:"), 3, 4, Qt::AlignVCenter);
	udpError = new QLabel("  0.000 %");
	lg->addWidget(udpError, 3, 5);

	/***************************************************************************/
	gbox = new QGroupBox("DBGU Calculation");
	lv->addWidget(gbox);

	lg = new QGridLayout(gbox);

	lg->addWidget(new QLabel("CD = "), 1, 0, Qt::AlignVCenter);

	lg->addWidget(new QLabel("MCK"), 0, 1, 1, 2, Qt::AlignVCenter | Qt::AlignHCenter);

	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 1, 1, 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("16 * Baud "), 2, 1, Qt::AlignVCenter);

	dbguBaud = new QSpinBox();
	dbguBaud->setRange(30, 4000000);
	dbguBaud->setValue(115200);
	lg->addWidget(dbguBaud, 2, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" = "), 1, 3, Qt::AlignVCenter | Qt::AlignRight);
	dbguCD = new QSpinBox();
	dbguCD->setRange(0, 65535);
	dbguCD->setReadOnly(true);
	lg->addWidget(dbguCD, 1, 4, Qt::AlignVCenter);

	lg->addWidget(new QLabel("Result"), 3, 0, Qt::AlignVCenter);
	lg->addWidget(new QLabel("Baud"), 3, 1, Qt::AlignVCenter);
	dbguBaudResult = new QDoubleSpinBox();
	dbguBaudResult->setRange(0.0, 4000000);
	dbguBaudResult->setReadOnly(true);
	dbguBaudResult->setDecimals(3);
	lg->addWidget(dbguBaudResult, 3, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("Error:"), 3, 3, Qt::AlignVCenter);
	dbguError = new QLabel("  0.000 %");
	lg->addWidget(dbguError, 3, 4, Qt::AlignVCenter);

	/***************************************************************************/
	gbox = new QGroupBox("USART Calculation");
	lv->addWidget(gbox);

	lg = new QGridLayout(gbox);

	lg->addWidget(new QLabel("Baud "), 1, 0, Qt::AlignVCenter);
	usartBaud = new QSpinBox();
	usartBaud->setRange(30, 7000000);
	usartBaud->setValue(115200);
	lg->addWidget(usartBaud, 1, 1, Qt::AlignVCenter);
	lg->addWidget(new QLabel(" = "), 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("MCK"), 0, 2, 1, 8, Qt::AlignVCenter | Qt::AlignHCenter);

	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 1, 3, 1, 8, Qt::AlignVCenter);

	lg->addWidget(new QLabel("8 * (2 - OVER"), 3, 3, Qt::AlignVCenter);
	usartOver = new QSpinBox();
	usartOver->setRange(0, 1);
	lg->addWidget(usartOver, 3, 4, Qt::AlignVCenter);

	lg->addWidget(new QLabel(") * (CD "), 3, 5, Qt::AlignVCenter);
	usartCD = new QSpinBox();
	usartCD->setRange(0, 65535);
	usartCD->setReadOnly(true);
	lg->addWidget(usartCD, 3, 6, Qt::AlignVCenter);

	lg->addWidget(new QLabel(" + "), 3, 7, Qt::AlignVCenter);
	lg->addWidget(new QLabel("FP"), 2, 8, Qt::AlignVCenter);
	usartFP = new QSpinBox();
	usartFP->setRange(0, 7);
	lg->addWidget(usartFP, 2, 9, Qt::AlignVCenter);

	f = new QFrame();
	f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	f->setFixedHeight(4);
	lg->addWidget(f, 3, 8, 1, 2, Qt::AlignVCenter);

	lg->addWidget(new QLabel("8"), 4, 8, 1, 2, Qt::AlignVCenter | Qt::AlignHCenter);
	lg->addWidget(new QLabel(")"), 3, 10, Qt::AlignVCenter);

	lg->addWidget(new QLabel("Result"), 5, 0, Qt::AlignVCenter);
	usartBaudResult = new QDoubleSpinBox();
	usartBaudResult->setRange(0.0, 7000000.0);
	usartBaudResult->setReadOnly(true);
	usartBaudResult->setDecimals(3);
	lg->addWidget(usartBaudResult, 5, 1, Qt::AlignVCenter);

	lg->addWidget(new QLabel("Error:"), 5, 8, Qt::AlignVCenter);
	usartError = new QLabel("  0.000 %");
	lg->addWidget(usartError, 5, 9, 1, 2, Qt::AlignVCenter);

	/***************************************************************************/
	connect(mainck, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(mul, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(div, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(mckPrescaler, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(udpPrescaler, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(dbguBaud, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(usartBaud, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(usartOver, SIGNAL(valueChanged(int)), this, SLOT(calc()));
	connect(usartFP, SIGNAL(valueChanged(int)), this, SLOT(calc()));

	/***************************************************************************/
	calc();
}

AT91SAM7S::~AT91SAM7S()
{
}

void AT91SAM7S::calc()
{
	calcPLL();
	calcMCK();
	calcUSB();
	calcDBGU();
	calcUSART();
}

void AT91SAM7S::calcPLL()
{
	double m, d, c, p;

	if (div->value() == 0) {
		pllck->setValue(0);
		return;
	}
	if (mul->value() == 0) {
		pllck->setValue(mainck->value());
		return;
	}
	c = mainck->value();
	m = mul->value() + 1;
	d = div->value();
	p = c * m / d;
	pllck->setValue(p);
}

void AT91SAM7S::calcMCK()
{
	mck->setValue(pllck->value() / (1 << mckPrescaler->value()));
}

void AT91SAM7S::usbGuess()
{
	double osc;
	double pllDiv;
	double pllMul;
	double pll;
	double usb;
	double delta;
	double res;
	int m, d, p, x;

	uProgress->setValue(0);
	osc = mainck->value();
	m = 0;
	d = 1;
	p = 0;
	res = 48000000.0;
	x = 0;
	for (int i = 0; i < 2048; i++) {
		pllMul = (double) i;
		for (int j = 1; j < 256; j++) {
			pllDiv = (double) j;
			pll = osc * (pllMul + 1.0) / pllDiv;
			if ((pll >= pllMin->value()) && (pll <= pllMax->value())) {
				for (int k = 0; k < 3; k++) {
					usb = pll / ((double)(1 << k));
					delta = (usb - 48000000.0) * 100.0 / 48000000.0;
					if (fabs(delta) < fabs(res)) {
						m = i;
						d = j;
						p = k;
						res = delta;
					}
				}
			}
			uProgress->setValue(i * 256 + j);
		}
	}
	mul->setValue(m);
	div->setValue(d);
	udpPrescaler->setValue(p);
	uProgress->setValue(2048 * 256);
	calc();
}

void AT91SAM7S::calcUSB()
{
	udpck->setValue(pllck->value() / (1 << udpPrescaler->value()));
	udpError->setText(QString("%1 %").arg((udpck->value() - 48000000.0) * 100.0 / 48000000.0, 6, 'f', 3));
}

void AT91SAM7S::calcDBGU()
{
	dbguCD->setValue(int(mck->value() / 16.0 / (double)dbguBaud->value()));
	if (dbguCD->value() == 0) {
		dbguBaudResult->setValue(0);
		dbguError->setText("xxx.xxx %");
	} else {
		dbguBaudResult->setValue(mck->value() / 16.0 / (double)dbguCD->value());
		dbguError->setText(QString("%1 %").arg((dbguBaudResult->value() - (double)dbguBaud->value()) * 100.0 / (double)dbguBaud->value(), 6, 'f', 3));
	}
}

double AT91SAM7S::usartSpeed(double clk, double cd, double over, double fp)
{
	return clk / ( 8.0 * (2.0 - over) * ( cd + (fp / 8.0) ) );
}

void AT91SAM7S::calcUSART()
{
	usartCD->setValue( (int)( (mck->value() / (8.0 * (2.0 - (double)usartOver->value())) / (double)usartBaud->value()) - ((double)usartFP->value() / 8.0) ) );
	if (usartCD->value() == 0) {
		usartBaudResult->setValue(0);
		usartError->setText("xxx.xxx %");
	} else {
		usartBaudResult->setValue(usartSpeed(mck->value(), usartCD->value(), usartOver->value(), usartFP->value()));
		usartError->setText(QString("%1 %").arg((usartBaudResult->value() - (double)usartBaud->value()) * 100.0 / (double)usartBaud->value(), 6, 'f', 3));
	}
}

/* End of file */
